//
//  RdbThemeSelectController.swift
//  rdb-led-banner
//
//  Created by radiobox3 on 2025/4/22.
//

import UIKit

class RdbThemeSelectController: RdbPanGestureController,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {

    let itemWith: CGFloat = (UIDevice.screenWidth() - 32 - 12 * 5) / 6
    
    let colors: [RdbColor.theme] = [
        .purple,
        .yellow,
        .green,
        .orange,
        .pink,
        .blue
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.canGesture = true
        self.view.backgroundColor = UIColor(white: 0, alpha: 0.5)
        self.contentView.backgroundColor = RdbColor.background.content.color
        
        let count = CGFloat(self.colors.count)
        var contentHeight = 80 + UIDevice.safeAreaBottom()
        if count > 0 {
            contentHeight = contentHeight + self.itemWith * (floor(CGFloat(self.colors.count / 6)))
        }
        
        self.contentView.snp.makeConstraints{
            $0.bottom.leading.trailing.equalToSuperview()
            $0.height.equalTo(contentHeight)
        }
        
        self.contentView.addSubview(self.titleTextLabel)
        self.titleTextLabel.snp.makeConstraints {
            $0.leading.equalTo(16)
            $0.top.equalTo(topIndicatorView.snp.bottom)
        }
        self.contentView.addSubview(collectionView)
        self.collectionView.snp.makeConstraints{
            $0.edges.equalTo(UIEdgeInsets(top: 60, left: 0, bottom: 0, right: 0))
        }
    }
    

    lazy var titleTextLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.white
        label.font = UIFont.semibold(20)
        label.text = self.title
        return label
    }()
    
    lazy var collectionView: UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.sectionInset = UIEdgeInsets(top: 5, left: 16, bottom: 15, right: 16)
        
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = RdbColor.background.content.color
        collectionView.isScrollEnabled = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "ThemeCollectionViewCell")
        return collectionView
    }()
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.colors.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let theme = self.colors[indexPath.item]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ThemeCollectionViewCell", for: indexPath)
        cell.layer.cornerRadius = self.itemWith / 2
        cell.backgroundColor = theme.color
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: self.itemWith , height: self.itemWith)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let theme: RdbColor.theme = self.colors[indexPath.item]
        RdbColor.shared.updateTheme(theme)
        NotificationCenter.default.post(name: .rdbThemeColorChange, object: nil)
        RdbUserDefaults.update(value: theme.rawValue, keyPath: .rdbThemeColor)
        self.dismissView()
    }
}
